﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student
{
    public partial class StuInfoManagementSystem : Form
    {
        public StuInfoManagementSystem()
        {
            InitializeComponent();
        }
        private void StuInfoManagementSystem_Load(object sender, EventArgs e)
        {
            //加载皮肤
            this.skinEngine1.Active = true;
            this.skinEngine1.SkinFile = "skin/Page.ssk";

            //加载所有信息
            var sql = "select * from StuInfo";
            var dt = DbHelper.GetSelect(sql);
            dataGridView1.DataSource = dt;

            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //只读
            dataGridView1.ReadOnly = true;
        }
        //增加
        private void btn_Insert_Click(object sender, EventArgs e)
        {
            //实例化增加信息窗体
            InsertInfo ins = new InsertInfo();
            //打开增加信息窗体
            ins.Show();
            //隐藏学生信息管理中心窗体
            this.Hide();
        }
        //删除
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                //获取用户选中的行
                var row = dataGridView1.SelectedRows[0];
                //获取用户选中行的Id列中的值
                var id = (int)row.Cells["Id"].Value;
                //再次确认是否删除
                var res = MessageBox.Show("确定删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                //获取窗体点击的返回值
                if (res.Equals(DialogResult.Yes))
                {
                    //确定删除
                    string sql = string.Format("delete from StuInfo where Id={0}", id);
                    var dt = DbHelper.GetIDU(sql);
                    //删除完更新数据
                    if (dt > 0)
                    {
                        string name = this.txt_Name.Text;
                        MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        var sql2 = "select * from StuInfo where Name='"+name+"'";
                        var dt2 = DbHelper.GetSelect(sql2);
                        dataGridView1.DataSource = dt2;
                    }
                }
            }
            else
            {
                MessageBox.Show("你没有选中任何的行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //修改
        private void btn_Update_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                //var sex = (string)dataGridView1.SelectedRows[0].Cells["Sex"].Value;
                var stuId = (int)dataGridView1.SelectedRows[0].Cells["StuId"].Value;
                //实例化修改信息窗体并进行传值
                UpdateInfo update = new UpdateInfo(id,name,stuId);
                //打开修改信息窗体
                update.Show();
                //显示修改信息窗体在最前面
                update.BringToFront();
                this.Hide();
            }
            else
            {
                MessageBox.Show("你没有选中任何的行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //退出
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
            Main main = new Main();
            main.Show();
        }
        //查询
        private void btn_Select_Click(object sender, EventArgs e)
        {
            //获取学号和名字
            string stuId = this.txt_StuId.Text.Trim();
            string name = this.txt_Name.Text.Trim();
            //当学号和姓名为空时
            if (stuId == "" && name == "")
            {
                string sql = "select * from StuInfo";
                var dt = DbHelper.GetSelect(sql);
                this.dataGridView1.DataSource = dt;
            }
            //但学号为空，姓名不为空时
            else if (stuId == "" && name != "")
            {
                string sql = string.Format("select * from StuInfo where Name like '%{0}%'", name);
                var dt = DbHelper.GetSelect(sql);
                this.dataGridView1.DataSource = dt;
            }
            //当姓名为空，学号不为空时
            else if (name == "" && stuId != "")
            {
                string sql = string.Format("select * from StuInfo where StuId like '%{0}%'", stuId);
                var dt = DbHelper.GetSelect(sql);
                this.dataGridView1.DataSource = dt;
            }
        }
    }
}
